package com.gnrsoft.springboot_test.commons.http;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import com.gnrsoft.springboot_test.commons.FastJsonConvert;

@Component
public class HttpService {

	@Resource
	private CloseableHttpClient httpClient;

	@Resource
	private RequestConfig config;

	/**
	 * 不带参数get
	 * 
	 * @param url
	 * @return
	 * @throws Exception
	 */
	public String doGet(String url) throws Exception {
		HttpGet httpGet = new HttpGet(url);
		httpGet.setConfig(config);
		CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
		if (httpResponse.getStatusLine().getStatusCode() == 200) {
			return EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
		}
		return null;
	}

	/**
	 * 带参数get
	 * 
	 * @param url
	 * @param parmMap
	 * @return
	 * @throws Exception
	 */
	public String doGet(String url, Map parmMap) throws Exception {

		URIBuilder uriBuilder = new URIBuilder(url);
		if (!CollectionUtils.isEmpty(parmMap)) {
			Iterator<Map.Entry> iterator = parmMap.entrySet().iterator();
			while (iterator.hasNext()) {
				Map.Entry entry = iterator.next();
				uriBuilder.setParameter(entry.getKey().toString(), entry.getValue().toString());
			}

		}
		return this.doGet(uriBuilder.build().toString());
	}

	/**
	 * 带参数post ContentType 为 application/json
	 * 
	 * @param url
	 * @param map
	 * @return
	 * @throws Exception
	 */
	public HttpResult doPostToJson(String url, Map parmMap) throws Exception {
		HttpPost httpPost = new HttpPost(url);
		httpPost.setConfig(config);
		if (!CollectionUtils.isEmpty(parmMap)) {

			StringEntity stringEntity = new StringEntity(FastJsonConvert.convertObjectToJSON(parmMap));
			stringEntity.setContentEncoding("UTF-8");
			stringEntity.setContentType(MediaType.APPLICATION_JSON_VALUE);
			httpPost.setEntity(stringEntity);
		}
		CloseableHttpResponse respone = this.httpClient.execute(httpPost);
		return new HttpResult(respone.getStatusLine().getStatusCode(),
				EntityUtils.toString(respone.getEntity(), "UTF-8"));
	}

	/**
	 * 带参数post 表单方式提交Post请求
	 * 
	 * @param url
	 * @param map
	 * @return
	 * @throws Exception
	 */
	public HttpResult doPostToForm(String url, Map parmMap) throws Exception {
		HttpPost httpPost = new HttpPost(url);
		httpPost.setConfig(config);

		if (!CollectionUtils.isEmpty(parmMap)) {
			List<NameValuePair> list = new ArrayList<NameValuePair>();
			Iterator<Map.Entry> iterator = parmMap.entrySet().iterator();
			while (iterator.hasNext()) {
				Map.Entry entry = iterator.next();
				list.add(new BasicNameValuePair(entry.getKey().toString(), entry.getValue().toString()));
			}

			UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(list);
			urlEncodedFormEntity.setContentEncoding("UTF-8");
			httpPost.setEntity(urlEncodedFormEntity);
		}

		CloseableHttpResponse respone = this.httpClient.execute(httpPost);
		return new HttpResult(respone.getStatusLine().getStatusCode(),
				EntityUtils.toString(respone.getEntity(), "UTF-8"));
	}

	
	/**
	 * 不带参数post
	 * 
	 * @param url
	 * @return
	 * @throws Exception
	 */
	public HttpResult doPost(String url) throws Exception {
		return this.doPostToForm(url, null);
	}

}
